package com.walle.ak47.commons.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

public class SimpleJdbcTemplate {

	private static Logger LOG = LoggerFactory
			.getLogger(SimpleJdbcTemplate.class);

	public static int updateQuietly(JdbcTemplate jdbcTemplate, String sql,
			Object... params) {
		try {
			return jdbcTemplate.update(sql, params);
		} catch (Throwable err) {
			LOG.error("DB update Error occured, {}", err.getMessage());
			if (LOG.isDebugEnabled())
				LOG.error(null, err);
			return 0;
		}
	}

	public static int batchUpdateQuietly(JdbcTemplate jdbcTemplate, String[] sql) {
		int totail = 0;
		try {
			int[] sizes = jdbcTemplate.batchUpdate(sql);
			for (int i : sizes) {
				totail += i;
			}
			return totail;
		} catch (Throwable err) {
			LOG.error("DB update Error occured, {}", err.getMessage());
			if (LOG.isDebugEnabled())
				LOG.error(null, err);
			return 0;
		}
	}
}
